/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.mensageiro.control;

import br.com.mensageiro.model.Contato;
import br.com.mensageiro.visual.Principal;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Danilo Dorotheu
 */
public class Kernel {
    private String host;
    private int port;
    private Socket cliente;
    private Recebedor r;
    private Principal principal;
    private Contato contato;
    
    public Kernel(String host, int port, Principal principal)
    {
        this.host = host;
        this.port = port;
        this.principal = principal;
    }
    
    public void inicializaCom()
    {
        try {
            cliente = new Socket(this.host, this.port);
            
        } catch (UnknownHostException ex) {
            Logger.getLogger(Kernel.class.getName()).log(Level.SEVERE, null, ex);
            
        } catch (IOException ex) {
            Logger.getLogger(Kernel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public boolean verificaStatus()
    {
        if(cliente.isConnected())
        {
            principal.txtStatus.setText("Conectado");
            return true;
        }else
        {
            principal.txtStatus.setText("Desconectado");
            return false;
        }
    }
    
    public void inicializaRecebedor()
    {
        //Thread que recebe as mensagens do servidor
        try {
            r = new Recebedor(cliente.getInputStream(), principal);
        } catch (IOException ex) {
            Logger.getLogger(Kernel.class.getName()).log(Level.SEVERE, null, ex);
        }
        new Thread(r).start();
    }
    
    public void inicializaStatus()
    {
        //Thread que fica verificando status do servidor
        Status status = new Status(principal, this);
        new Thread(status).start();
    }
    
    public void enviarMensagem(String mensagem)
    {
        try {
            Scanner sc = new Scanner(mensagem);
            PrintStream saida = new PrintStream(cliente.getOutputStream());
            
            while(sc.hasNextLine())
            {
                saida.println(getUser()+": "+sc.nextLine());
            }
            
        } catch (IOException ex) {
            Logger.getLogger(Kernel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String getUser()
    {
        return contato.getNome();
    }
    
    public void setUser()
    {
        contato = new Contato();
        String nome = "Default user";
        
        try
        {
            BufferedReader lerArq = new BufferedReader(new FileReader("src/Config/Configuracoes.txt"));
            contato.setNome(lerArq.readLine());
        }catch(IOException e)
        {}
    }
}
